<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Tab;
use App\Models\Project;
use App\Models\Layer;
use Dcat\Admin\Show;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid\Displayers\Actions;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Support\Helper;

// use Dcat\Admin\Admin;
// use Dcat\Admin\Form;
use Dcat\Admin\Http\Repositories\Administrator;
// use Dcat\Admin\Layout\Content;
use Dcat\Admin\Traits\HasFormResponse;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Http\Request;
// use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;

class LayerController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('楼层列表')
            ->description()
//            ->body(Callout::make('即将在下个版本发布，敬请期待~'))
           // ->body($this->build())
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new Layer(), function (Grid $grid) {
            $grid->disableColumnSelector();
            // $grid->showColumnSelector();
            $grid->column('layerId','ID');
            $grid->column('layername','楼层名称')->limit(50);
            // $grid->column('remark','项目说明');
            $grid->column('created_at','创建时间');
            $grid->column('projectId','项目名称')->display(function($projectId) {
                return Project::where(['projectId'=>$projectId])->value('projectname');;
            });
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            $grid->disableViewButton();
            // $grid->showViewButton();
            //操作按钮
            $grid->showActions();
            // $grid->disableActions();
            // $grid->disableBatchDelete();
            // 创建按钮
            $grid->showCreateButton();
            // 禁用创建按钮
            // $grid->disableCreateButton();
            // 禁用详情按钮
            // $grid->disableViewButton();
            // 禁用编辑按钮
            // $grid->disableEditButton();
            // 显示编辑按钮
            $grid->showEditButton();
            // 禁用快捷编辑按钮
            $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            // $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            // $grid->enableDialogCreate();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            // $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            //     $create->text('projectname', '项目名称');
            //     $create->text('remark', '项目说明');
            //     $create->select('projectId', '所属公司')->options([
            //         1 => 'foo',
            //         2 => 'bar',
            //     ]);
            //     $create->decimal('lat', '纬度');
            //     $create->decimal('lng', '经度');
            // });
            // 显示删除按钮
            // $grid->showDeleteButton();
        });
    }
    public function handle(array $input)
    {
        return $this->success('保存成功');
    }
    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建楼层')
            ->description('创建')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form());
    }
    public function form(){
        return Form::make(new Layer(), function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(9, 2);
                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('layername','楼层名称')->required();
                    $form->text('remark','楼层说明');
                    $res = Project::where('user_id',Admin::user()->id)->pluck('projectname','projectId');
                    $form->select('projectId', '所属项目')->options($res)->required();
                    // $form->decimal('lat','纬度');
                    // $form->decimal('lng','经度');
                    $form->image('planeJPG','平面底图');
                });
            });
        });
    }

    public function show($id){
        return Show::make($id, new Layer(), function (Show $show) {
            $show->layerId;
            $show->field('layername','楼层名称');
            $show->field('remark','楼层名称');
            $show->created_at;
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('楼层编辑')
            ->description('编辑')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form()->edit($id));
    }
}